#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys

import requests
from pymongo import MongoClient
from scpy.logger import get_logger
from scpy.qiniu import Qiniu
from xtls.codehelper import no_exception
from xtls.errors import ConsumerFatalError
from xtls.mqwrapper import consumer
from xtls.util import BeautifulSoup

reload(sys)
sys.setdefaultencoding("utf-8")
logger = get_logger(__file__)
qiniu = Qiniu(bucket='sc-crawler', host='10.132.23.104')
MONGO = MongoClient('10.132.23.104', 27017)


@no_exception((500, 'err'))
def deal_task(param):
    for _ in [1, 2, 3]:
        try:
            content = requests.get(param['downloadUrl'], headers={
                'Host': 'www.cninfo.com.cn',
                'Upgrade-Insecure-Requests': 1,
                'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36'
            })
            if content.status_code == 200:
                content = content.content
                break
        except Exception, e:
            pass
    else:
        return 404, ''

    rst = qiniu.upload(content, suffix='pdf')

    MONGO['stockProj']['shenhuStockAnno'].update_one(
        {'_id': param['_id']},
        {'$set': {'content': '', 'fileKey': rst['key']}}
    )
    return 200, 'ok'


@consumer('10.117.29.191', 'stock_anno_download', logger=logger)
def task(param):
    logger.info(u'recv : %s' % param)
    code, msg = deal_task(param)
    if code == 500:
        raise ConsumerFatalError(u'退出 %s' % param)
    if code == 404:
        MONGO['stockProj']['pdfErr'].update_one({'_id': param['_id']}, {'$set': param}, upsert=True)
    return code, msg

if __name__ == '__main__':
    task()
